package com.ly.hotel.view.setting;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.view.setting.SysinfoLayout.NetInfo;


public class UserGuideActivity extends Activity {

	private ImageButton settingBtn;
	private TextView ipTips;
	private TextView maskTips;
	private TextView macTips;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_user_guide);
		MyApplication app = (MyApplication) MyApplication.getContext();
		NetInfo info = getInfo();
		ipTips = findViewById(R.id.ipTips);
		maskTips = findViewById(R.id.maskTips);
		macTips = findViewById(R.id.macTips);
		
		ipTips.setText(app.getMyIpAddr());
		maskTips.setText(info.networkMask);
		macTips.setText(MyApplication.getMAC());
		
		settingBtn = findViewById(R.id.settingBtn);

		settingBtn.setOnClickListener(new View.OnClickListener() {
			// 进入系统设置增加密码 By小朱 2014/11/4
			@Override
			public void onClick(View v) {

				Builder builder = new Builder(
						UserGuideActivity.this);

				builder.setTitle(getString(R.string.PasswordTips));
				final EditText editText = new EditText(UserGuideActivity.this);
				editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				editText.setLines(1);
				editText.addTextChangedListener(new TextWatcher() {
					@Override
					public void beforeTextChanged(CharSequence s, int start, int count, int after) {

					}

					@Override
					public void onTextChanged(CharSequence s, int start, int before, int count) {

					}

					@Override
					public void afterTextChanged(Editable s) {
						if (editText.getText().toString().trim().equals("6111")) {
							UserGuideActivity.this.startActivity(new Intent(
									UserGuideActivity.this,
									SystemSettingActivity.class));
						}
					}
				});
				builder.setView(editText);

				builder.setPositiveButton(getString(R.string.yes), new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {

						if (editText.getText().toString().trim().equals("6111")) {
							UserGuideActivity.this.startActivity(new Intent(
									UserGuideActivity.this,
									SystemSettingActivity.class));
						} else {
							Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
						}

					}
				});
				builder.setNegativeButton(getString(R.string.no), new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {

					}
				});

				builder.create().show();
			}
		});
	}
	
	private NetInfo getInfo() {
		InputStream is = null;
		List<String> commandResult = new ArrayList<String>();
		Process exec = null;
		try {
			 exec = Runtime.getRuntime().exec("getprop");
			is = exec.getInputStream();
			if (is != null) {
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line = null;
				while((line = br.readLine()) != null) {
					commandResult.add(line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			exec.destroy();
		}
		
		NetInfo info = new NetInfo();
		for (String s : commandResult) {
			//[dhcp.eth0.ipaddress]: [192.168.2.112]
			if (s.contains("[dhcp.eth0.ipaddress]")) {//ip地址
				info.ip =  s.replace("[dhcp.eth0.ipaddress]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.gateway]")) {  //网关 [dhcp.eth0.gateway]: [192.168.2.1]
				info.gateway =  s.replace("[dhcp.eth0.gateway]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.mask]")) {  //子网掩码 [dhcp.eth0.mask]: [255.255.255.0]
				info.networkMask =  s.replace("[dhcp.eth0.mask]", "").replace(": ", "").replace("[", "").replace("]", "");
			} else if (s.contains("[dhcp.eth0.dns")) {  //dns [dhcp.eth0.dns1]: [192.168.2.1]
				String dns = s.split(": ")[1].replace("[", "").replace("]", "");
				if (!dns.trim().equals("")) {
					info.dns = dns + "  ";
				}
			}
		}
		return info;
	}
}
